let editor = document.getElementById("editor").children[0];
function setBtn(id, styleName){
    let elm = document.getElementById(id);
    elm.onclick = function(){
        editor.style[styleName] = id;
    }
}
setBtn("left", "textAlign");
setBtn("center", "textAlign");
setBtn("right", "textAlign");

function setMenu(id, styleName){
    let elm = document.getElementById(id);
    function change(){
        editor.style[id] = elm.value || elm.children[elm.selectedIndex].innerHTML;
    }
    change();
    elm.onchange = change;
}
setMenu("color");
setMenu("fontSize");
setMenu("fontFamily");

function setChecked(id, styleName){
    let elm = document.getElementById(id);
    function change(){
        editor.style[elm.name] = elm.checked ? this.value : "";
    }
    change();
    elm.onchange = elm.onpropertychange = change;
}
setChecked("bold");
setChecked("italic");
setChecked("underline");
setChecked("left");
setChecked("center");
setChecked("right");

let link = document.getElementById("lnk"),
    img = document.getElementById("img");
img.onclick = function(){
    let url = prompt("请输入图片 url", "http://"),
        img;
    if(url){
        img = new Image();
        img.src = url;
        editor.appendChild(img)
    }
}
link.onclick = function(){
    let url = prompt("请输入链接 url", "http://"),
        lnk;
    if(url){
        lnk = document.createElement("a");
        lnk.href = url;
        lnk.innerHTML = prompt("请输入链接文字或者点击取消", "") || url;
        editor.appendChild(lnk)
    }
}